﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Web.Configuration;
using System.Configuration;
using Ebiz.libs;

namespace Ebiz
{
    public partial class SetupDB : System.Web.UI.Page
    {
        public string strPostMsg;
        protected void Page_Load(object sender, EventArgs e){
            strPostMsg = "";
            if (!IsPostBack){
                getServerList();
            }else if (IsPostBack){
                int errFlag = 0;

                string txtSrvName = txtServer.Text;
                if (string.IsNullOrEmpty(txtSrvName)) { errFlag = 1; }

                //string txtUname = txtUsername.Text;
                //if (string.IsNullOrEmpty(txtUname)) { errFlag = 1; }
                string txtUname = txtUsername.Text;
                txtUsername.Attributes.Add("value", txtUname);

                string txtPass = txtPassword.Text;
                txtPassword.Attributes.Add("value", txtPass);

                string txtDB = txtDatabase.Text;
                if (string.IsNullOrEmpty(txtDB)) { errFlag = 1; }

                DBUtilities.ConnectionMode eLogMode = new DBUtilities.ConnectionMode();
                string LogMode = cbAuthMode.SelectedValue.ToString();
                switch (LogMode) { 
                    case "0": case "1":
                        eLogMode = DBUtilities.ConnectionMode.AuthWindows;
                        break;
                    case "2":
                        eLogMode = DBUtilities.ConnectionMode.AuthSQL;
                        break;
                }

                string strCon = "";
                if (errFlag == 0){
                    Dictionary<string, object> PostFields = new Dictionary<string, object>();
                    PostFields["ServerName"] = txtSrvName;
                    PostFields["UserName"] = txtUname;
                    PostFields["Password"] = txtPass;
                    PostFields["DatabaseName"] = txtDB;

                    strCon = DBUtilities.SetConnectionString(PostFields, eLogMode);
                    if (DBUtilities.CheckConnection(strCon) == true){
                        ChangeSetting(strCon);
                        //strPostMsg = strCon;
                        Response.Write("<script>alert('Configuration Success!'); window.location.href='Default.aspx';</script>");
                    }else{
                        strPostMsg = "Database connection failed. Please check your configuration.";
                        //strPostMsg = strCon;
                    }
                }else{
                    strPostMsg = "Setup Error: Please check your configuration!";
                    //strPostMsg = strCon;
                }
            }
        }

        protected void getServerList(){
            cbServer.Items.Add(new ListItem("--- Select ---", "0"));
            DataTable dt = SqlDataSourceEnumerator.Instance.GetDataSources();
            int idx = 0;
            foreach (DataRow lstServers in dt.Rows){
                idx++;
                cbServer.Items.Add(string.Concat(lstServers["ServerName"], "\\", lstServers["Instancename"]));
            }
        }

        protected void ChangeSetting(string strConnection){
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

            int cstrCount = ConfigurationManager.ConnectionStrings.Count;
            string xsName = "devCon";
            string strCon = strConnection;

            //ConnectionStringSettings csSetting = new ConnectionStringSettings(xsName, strCon, "System.Data.SqlClient");
            ConnectionStringSettings csSetting = new ConnectionStringSettings(xsName, strCon);
            ConnectionStringsSection csSection = config.ConnectionStrings;
            csSection.ConnectionStrings.Clear();
            csSection.ConnectionStrings.Add(csSetting);
            config.Save(ConfigurationSaveMode.Full);
            ConfigurationManager.RefreshSection("connectionStrings");
        }

    }
}